package com.jbm.jbmsupplier.zp.presenters;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.jbm.jbmsupplier.zp.entity.Buddy_ListData;
import com.jbm.jbmsupplier.zp.entity.Recent_ContactsData;
import com.jbm.jbmsupplier.zp.models.RecentInterfaces;
import com.jbm.jbmsupplier.zp.utils.ClassUser;
import com.jbm.jbmsupplier.zp.views.RecentViews;

import java.util.ArrayList;

import static com.jbm.jbmsupplier.zp.sqldata.MySqliteUtlis.GetInstan;
import static com.jbm.jbmsupplier.zp.utils.TableName.BUDDY;
import static com.jbm.jbmsupplier.zp.utils.TableName.CHAT;
import static com.jbm.jbmsupplier.zp.utils.TableName.CHATGROUD;
import static com.jbm.jbmsupplier.zp.utils.TableName.GROUD;
import static com.jbm.jbmsupplier.zp.utils.TableName.RECENT;

/**
 * Created by 家帮忙 on 2017/7/12.
 */

public class RecentActivitys implements RecentInterfaces {
    private Context context;
    private RecentViews recentViews;
    private ClassUser classUser;
    private MyBroadCat broa=null;
    private ArrayList<Recent_ContactsData> list2;
    private IntentFilter file = null;
    private ArrayList<String> als;
    private int to_id;
    private  int UnreadMsg;
    private ArrayList<Recent_ContactsData> list = null;
    private boolean ISonResume;
    private Buddy_ListData BuddyListData;

    public RecentActivitys(Context context, RecentViews recentViews) {
        this.context = context;
        this.recentViews = recentViews;
    }

    @Override
    public void InitData() {
        classUser=new ClassUser(context);
        list2 = new ArrayList<>();

        recentViews.LoadData(list2,classUser);//将集合传入适配器
    }

    @Override
    public void RequestChat() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                updateMsg();
            }
        }).start();
    }

    @Override
    public void OnStop() {//停止后销毁广播
        if (broa!=null){
            context.unregisterReceiver(broa);
            broa=null;
        }

    }

    @Override
    public void regisBroadcard() {//注册需要的广播
        file = new IntentFilter();
        //接收消息广播
        file.addAction("MSG");
//        //小秘书
//        file.addAction("CG");
        //添加好友
        file.addAction("AddContact");
        //删除好友
        file.addAction("DropContact");
        //删除消息好友
        file.addAction("DropContactss");
        if(broa==null){
            broa = new MyBroadCat();
            context. registerReceiver(broa, file);}
    }

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            recentViews.Updata(list2,1);
        }
    };
    class MyBroadCat extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            /**
             * 0  代表文本
             * 1  开门
             * 2
             */
            Log.e("main","广播执行");
            switch (intent.getIntExtra("messageFlag",0)){
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
                case 7:
                    break;
                case 8:
                    updateMsg();
                    break;
                case 10:
                    updateMsg();
                    break;
                case 12:
                    updateMsg();
                    break;
                case 14:
                    updateMsg();
                    break;
                case 16:
                    updateMsg();
                    break;
                case 50:
                    updateMsg();
                    break;
            }
        }
    }

    public void updateMsg(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                ShowGroadMsg();
                try {
                    als = GetInstan(context).showaRecentContactsids(RECENT,classUser.getLogin());
                    for (int i = 0;i<als.size();i++) {
                        list = GetInstan(context).showRecentContactslast(CHAT,classUser.getLogin(),Integer.parseInt(als.get(i)));
                        UnreadMsg = GetInstan(context).ChatUnreadMsg(CHAT,classUser.getLogin(),Integer.parseInt(als.get(i)));
                        BuddyListData = GetInstan(context).showFriendsToID(BUDDY,Integer.parseInt(als.get(i)),classUser.getLogin());
                        Recent_ContactsData recent_contactsData = new Recent_ContactsData();
                        if (BuddyListData!=null){
                        String headimage=BuddyListData.getHead_portrait();
                        String name= BuddyListData.getNickname();
                        to_id = Integer.parseInt(als.get(i));
                            recent_contactsData.setName(name);
                            recent_contactsData.setHead_portrait(headimage);
                        }else{
                            recent_contactsData.setName("");
                            recent_contactsData.setHead_portrait("");
                        }
                            recent_contactsData.setMsg(list.get(0).getMsg());
                            recent_contactsData.setTime(list.get(0).getTime());
                            recent_contactsData.setUnreadMsg(UnreadMsg);
                            recent_contactsData.setTo_id(to_id);
                            list2.add(recent_contactsData);
                    }

                    handler.sendEmptyMessage(0x0111);
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        }).start();
    }
    public void SetOnStar(boolean Is){
//        ISonResume=Is;
    }
public void ShowGroadMsg(){
    ArrayList<Recent_ContactsData> tablegroad = GetInstan(context).ShowGroadTables(GROUD,classUser.getLogin());
    if(tablegroad!=null) {
        list2.clear();
        for (int q = 0; q < tablegroad.size(); q++) {
            int weireadmsg=GetInstan(context).Selectreadmsg(CHATGROUD,tablegroad.get(q).getCgid());
            Recent_ContactsData recent_contactsDataq = new Recent_ContactsData();
            recent_contactsDataq.setName(tablegroad.get(q).getName());
            recent_contactsDataq.setHead_portrait(tablegroad.get(q).getHead_portrait());
            recent_contactsDataq.setMsg(tablegroad.get(q).getMsg());
            recent_contactsDataq.setTime(tablegroad.get(q).getTime());
            recent_contactsDataq.setUnreadMsg(tablegroad.get(q).getUnreadMsg());
            recent_contactsDataq.setCgid(tablegroad.get(q).getCgid());
            recent_contactsDataq.setTag(tablegroad.get(q).getTag());
            recent_contactsDataq.setTo_id(tablegroad.get(q).getTo_id());
            recent_contactsDataq.setUnreadMsg(weireadmsg);
            list2.add(recent_contactsDataq);
        }}else{
        list2.clear();
    }
}


}
